﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Net;
using System.IO;
using System.Timers;



namespace Monitor_
{

    public partial class Monitor_ : ServiceBase
    {
        private Timer aTimer;

        static void Main()
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Monitor_() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }

        public Monitor_()
        {
            InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists("MySource"))
                System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog");
            eventLog1.Source = "MySource";
            eventLog1.Log = "MyNewLog";

            aTimer = new Timer(900000);
            aTimer.Elapsed += new ElapsedEventHandler(Timer_Tick);
            aTimer.Interval = 900000;
            aTimer.Enabled = false;
        }

        protected override void OnStart(string[] args)
        {
            eventLog1.WriteEntry("Monitor Service was started");
            aTimer.Enabled = true;
        }

        protected override void OnStop()
        {
            eventLog1.WriteEntry("Monitor Service was stopped");
            aTimer.Enabled = false;
        }

        private void Timer_Tick(object source, ElapsedEventArgs e)
        {
            WebRequest wReq = WebRequest.Create(@"http://www.bloomberg.com/markets/currencies/");
            WebResponse wResp = wReq.GetResponse();
            Stream respStream = wResp.GetResponseStream();
            StreamReader reader = new StreamReader(respStream, Encoding.ASCII);
            string respHTML = reader.ReadToEnd();

            int BeginPos = respHTML.IndexOf("<h2>Major World Currencies</h2>");
            int EndPos = respHTML.LastIndexOf("<div class='stock_chart'>") - BeginPos;
            respHTML = respHTML.Substring(BeginPos, EndPos);

            string Value;

            respHTML = respHTML.Substring(respHTML.IndexOf("EUR-USD"));
            respHTML = respHTML.Substring(8 + respHTML.IndexOf("'value'>"));
            Value = respHTML.Substring(0, respHTML.IndexOf("</td>"));
            //add EUR-USD && Value
            eventLog1.WriteEntry("EUR-USD " + Value);

            respHTML = respHTML.Substring(respHTML.IndexOf("GBP-USD"));
            respHTML = respHTML.Substring(8 + respHTML.IndexOf("'value'>"));
            Value = respHTML.Substring(0, respHTML.IndexOf("</td>"));
            //add GBP-USD && Value
            eventLog1.WriteEntry("GBP-USD " + Value);

            respHTML = respHTML.Substring(respHTML.IndexOf("USD-JPY"));
            respHTML = respHTML.Substring(8 + respHTML.IndexOf("'value'>"));
            Value = respHTML.Substring(0, respHTML.IndexOf("</td>"));
            //add USD-JPY && Value
            eventLog1.WriteEntry("USD-JPY " + Value);

            respHTML = respHTML.Substring(respHTML.IndexOf("AUD-USD"));
            respHTML = respHTML.Substring(8 + respHTML.IndexOf("'value'>"));
            Value = respHTML.Substring(0, respHTML.IndexOf("</td>"));
            //add AUD-USD && Value
            eventLog1.WriteEntry("AUD-USD " + Value);

            respHTML = respHTML.Substring(respHTML.IndexOf("USD-CAD"));
            respHTML = respHTML.Substring(8 + respHTML.IndexOf("'value'>"));
            Value = respHTML.Substring(0, respHTML.IndexOf("</td>"));
            //add USD-CAD && Value
            eventLog1.WriteEntry("USD-CAD " + Value);

            respHTML = respHTML.Substring(respHTML.IndexOf("USD-CHF"));
            respHTML = respHTML.Substring(8 + respHTML.IndexOf("'value'>"));
            Value = respHTML.Substring(0, respHTML.IndexOf("</td>"));
            //add USD-CHF && Value
            eventLog1.WriteEntry("USD-CHF " + Value);

            respHTML = respHTML.Substring(respHTML.IndexOf("EUR-GBP"));
            respHTML = respHTML.Substring(8 + respHTML.IndexOf("'value'>"));
            Value = respHTML.Substring(0, respHTML.IndexOf("</td>"));
            //add EUR-GBP && Value
            eventLog1.WriteEntry("EUR-GBP " + Value);

            respHTML = respHTML.Substring(respHTML.IndexOf("EUR-JPY"));
            respHTML = respHTML.Substring(8 + respHTML.IndexOf("'value'>"));
            Value = respHTML.Substring(0, respHTML.IndexOf("</td>"));
            //add EUR-JPY && Value
            eventLog1.WriteEntry("EUR-JPY " + Value);

            respHTML = respHTML.Substring(respHTML.IndexOf("AUD-JPY"));
            respHTML = respHTML.Substring(8 + respHTML.IndexOf("'value'>"));
            Value = respHTML.Substring(0, respHTML.IndexOf("</td>"));
            //add AUD-JPY && Value
            eventLog1.WriteEntry("AUD-JPY " + Value);
        }

    }


}
